package com.xca.okhttp.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.xca.okhttp.common.WeakRrfHandler;
import com.xca.okhttp.contract.IHotSearchContact;
import com.xca.okhttp.entity.HotSearch;
import com.xca.okhttp.service.HotSearchService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HotSearchPresenter implements IHotSearchContact.HotSearchPresenter {
    private static final int HTTP_SUCCESS = 1 ;
    public static final int HTTP_SUCCESS2 = 2;
    private static final int HTTP_FAIL = 0 ;
    private IHotSearchContact.HotSearchView mMainView ;
    private Context mContext ;
    private OkHttpClient mClient ;

    public HotSearchPresenter(IHotSearchContact.HotSearchView mMainView , Context mContext){
        this.mMainView = mMainView ;
        this.mContext = mContext ;
        mClient = new OkHttpClient();
    }
    @Override
    public void getData(String keyWord) {
        //Retorfit获取方式
        String url = "https://api.oioweb.cn/api/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HotSearchService hotSearchService = retrofit.create(HotSearchService.class);

        hotSearchService.getData6(keyWord).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String body = response.body().string();
                    String url = call.request().url().toString() ;
                    Message message = Message.obtain() ;
                    message.obj = new String[]{body , url};
                    message.what = HTTP_SUCCESS ;
                    mHandler.sendMessage(message);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

        //方法1
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Response<ResponseBody> response = hotSearchService.getData(keyWord).execute();
//                    if (response.isSuccessful()){
//                        String body  = response.body().string();
//                        Message message = Message.obtain();
//                        message.obj = new String[]{body , url} ;
//                        message.what = HTTP_SUCCESS ;
//                        mHandler.sendMessage(message);
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }).start();

        //方法2
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Response<HotSearch> response = hotSearchService.getData2(keyWord).execute();
//                    if (response.isSuccessful()){
//                        HotSearch hotSearch = response.body();
//                        Message message = Message.obtain();
//                        message.obj = hotSearch ;
//                        message.what = HTTP_SUCCESS2 ;
//                        mHandler.sendMessage(message);
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }).start();

        //方法3 不能用于表单
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Map<String , Object> map = new HashMap<>() ;
//                    map.put("msg" , keyWord);
//                    Response<HotSearch> response = hotSearchService.getData3(map).execute();
//                    if (response.isSuccessful()){
//                        HotSearch hotSearch = response.body();
//                        Message message = Message.obtain() ;
//                        message.obj = hotSearch ;
//                        message.what = HTTP_SUCCESS2 ;
//                        mHandler.sendMessage(message);
//                    }
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }).start();


        //OkHttp获取方式
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Request request = new Request.Builder()
//                        .url("https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=" + keyWord)
//                        .build() ;
//                Call call = mClient.newCall(request);
//                call.enqueue(new Callback() {
//                    @Override
//                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
//
//                    }
//
//                    @Override
//                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                        String body = response.body().string() ;
//                        String url = call.request().url().toString() ;
//                        Message message = Message.obtain() ;
//                        message.obj = new String[]{body , url} ;
//                        message.what = HTTP_SUCCESS ;
//                        mHandler.sendMessage(message);
//                    }
//                });
//            }
//        }).start();
    }

    WeakRrfHandler<HotSearchPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    String[] data = (String[]) msg.obj;
                    if (data == null || data.length < 2) {
                        return false;
                    }
                    String jsonStr = data[0];
                    Gson gson = new Gson();
                    HotSearch hotSearchEntity = gson.fromJson(jsonStr, HotSearch.class);
                    if (mMainView != null) {
                        mMainView.showData(hotSearchEntity);
                    }
                    break;
                case HTTP_SUCCESS2:
                    if (mMainView != null) {
                        mMainView.showData((HotSearch) msg.obj);
                    }
                    break;
                case HTTP_FAIL:
                    break;
            }
            return false;
        }
    });
}
